package com.neusoft.lohas.modules.ivm.entity.business;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.neusoft.lohas.common.persistence.IdEntity;
import com.neusoft.lohas.common.utils.DateUtils;

/**
 * 商圈活动时间类
 * 
 * @author jackie.liu
 * @version 2015-02-10
 */
@Entity
@Table(name = "ivm_business_activity_time")
@DynamicInsert
@DynamicUpdate
public class ActivityTime extends IdEntity<ActivityTime> {

	private static final long serialVersionUID = 1L;
	private Activity activity;
	private double amount; //赠送活动使用的上线金额
	private Integer type; //赠送活动周期类型：0非赠送活动，1为日周期，2为周周期
	private Integer week; //周期类型为周时的星期：0非周周期类型的赠送活动，1为星期一，2为星期二，以此类推
	private Date activityDate; //活动日期：yyyy-MM-dd
	private String startTime;
	private String endTime;
	private Date expiryDate; //赠送活动的截止日期
	private Integer sort;
	private float ratio;  //售空比例
	private int state; //抢购状态：1为即将开始，2为正在抢购，3为已结束
	private List<ActivityProduct> productList = Lists.newArrayList();

	public ActivityTime() {
		super();
	}

	public ActivityTime(String id) {
		this();
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name="activity_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	@NotNull(message="所属活动信息不能为空")
	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Integer getWeek() {
		return week;
	}

	public void setWeek(Integer week) {
		this.week = week;
	}

	public Date getActivityDate() {
		return activityDate;
	}

	public void setActivityDate(Date activityDate) {
		this.activityDate = activityDate;
	}

	@Transient
	public String getActivityDateStr() {
		if (activityDate!=null && activityDate.getTime()>0) {
			return DateUtils.formatDate(activityDate, DateUtils.parsePatterns[0]);
		}
		return "";
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public Date getExpiryDate() {
		return expiryDate;
	}

	public void setExpiryDate(Date expiryDate) {
		this.expiryDate = expiryDate;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}
	
	@Transient
	public float getRatio() {
		return ratio;
	}

	public void setRatio(float ratio) {
		this.ratio = ratio;
	}

	@Transient
	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "activityTime")
	@Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'")
	@OrderBy("sort")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	public List<ActivityProduct> getProductList() {
		return productList;
	}

	public void setProductList(List<ActivityProduct> productList) {
		this.productList = productList;
	}
}
